﻿using SD.Infrastructure.Repository.EntityFrameworkCore;
using SlamDunk.MES.Domain.Entities.AcquisitionContext;
using SlamDunk.MES.Domain.IRepositories.Interfaces.AcquisitionContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SlamDunk.MES.Repository.Implements.AcquisitionContext
{
    /// <summary>
    /// 采集规则仓储实现
    /// </summary>
    public class AcquisitionRuleRepository : EFAggRootRepositoryProvider<AcquisitionRule>, IAcquisitionRuleRepository
    {
        #region # 分页获取采集规则列表 —— ICollection<AcquisitionRule> FindByPage(string keywords...
        /// <summary>
        /// 分页获取采集规则列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="processId">工艺Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>采集规则列表</returns>
        public ICollection<AcquisitionRule> FindByPage(string keywords, Guid? processId, Guid? processSegmentId, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<AcquisitionRule, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (processId == null || x.ProcessId == processId) &&
                    (processSegmentId == null || x.ProcessSegmentId == processSegmentId);

            IQueryable<AcquisitionRule> acquisitionRules = base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount);

            return acquisitionRules.ToList();
        }
        #endregion

        #region # 获取工艺段相关采集规则 —— AcquisitionRule SingleByProcessSegment(Guid processSegmentId)
        /// <summary>
        /// 获取工艺段相关采集规则
        /// </summary>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <returns>采集规则</returns>
        public AcquisitionRule SingleByProcessSegment(Guid processSegmentId)
        {
            AcquisitionRule acquisitionRule = base.SingleOrDefault(x => x.ProcessSegmentId == processSegmentId);

            return acquisitionRule;
        }
        #endregion

        #region # 是否存在工艺段相关采集规则 —— bool ExistsByProcessSegment(Guid processSegmentId)
        /// <summary>
        /// 是否存在工艺段相关采集规则
        /// </summary>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <returns>是否存在</returns>
        public bool ExistsByProcessSegment(Guid processSegmentId)
        {
            return base.Exists(x => x.ProcessSegmentId.HasValue && x.ProcessSegmentId.Value == processSegmentId);
        }
        #endregion
    }
}
